#coding=utf-8
#从数据库获取对应的接口信息，进行测试并且对比期望值，给出测试结果
import requests
import Get_datas
import random
from TestWeb import ID_card
from TestWeb import register
from TestWeb import mysql_password
from TestWeb import platform_login_api
from TestWeb import purchaseApply
from TestWeb import reply
from TestWeb import mysql_connect
import unittest
s = requests.session()
base_url = 'http://community-platform-api-test.gdgyun.com'
article_url = 'http://community-article-api-test.gdgyun.com'


print '开始朵云社区接口自动化测试^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
def test(id,phone,encryptedPassword,code,salt):
    case_name = Get_datas.Get_testdatas(id ,'case_name')
    url = Get_url(id,phone)
    method = Get_datas.Get_testdatas(id, 'method')
    headers = Get_headers(id,phone)
    param = Get_param(id,phone,encryptedPassword,code,salt)
    expected = Get_datas.Get_testdatas(id, 'expected')
    actual_result = Get_datas.Get_testdatas(id, 'actual_result')
    result = Get_datas.Get_testdatas(id, 'result')
    #取出expected中的code值
    newexpected = eval(expected)
    expected_code = newexpected["code"]
    print u'开始接口测试：'+case_name
    #请求查询到的接口
    if 25 < int(id) < 66:
        head_url = article_url
        if method == 'get':
            test_api = s.get(head_url+url,params=param,headers=headers)
            print test_api.text  #输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id,test_api.text)  #更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'post':
            test_api = s.post(head_url+url,json=param,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'put':
            test_api = s.put(head_url+url,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'delete':
            test_api = s.delete(head_url+url,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

    else:
        head_url = base_url
        if method == 'get':
            test_api = s.get(head_url+url,params=param,headers=headers)
            print test_api.text  #输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'post':
            test_api = s.post(head_url+url,json=param,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'put':
            test_api = s.put(head_url+url,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)

        if method == 'delete':
            test_api = s.delete(head_url+url,headers=headers)
            print test_api.text  # 输出实际测试结果
            result_code = test_api.json()["code"]
            if expected_code != result_code:
                update(id, test_api.text)  # 更新实际结果
                update_fail(id)
            if expected_code == result_code:
                update(id, test_api.text)  # 更新实际结果
                update_pass(id)





#从数据库获取param，并且进行参数化
def Get_param(id,phone,encryptedPassword,code,salt):
    param = Get_datas.Get_testdatas(id, 'param')
    #param = eval(param)
    if "${code}" in param or "${pwd}" in param or "${salt}" in param or "${phone}" in param or "${uid}" in param or "${idCardNo}" in param or "${realName}"in param or "${nickName}" in param:
        uid = mysql_password.password(phone)[1]
        username = str(register.name())
        idCardNo = str(ID_card.idcard())
        newparam = param.replace("${phone}", phone).replace("${code}", code).replace("${pwd}", encryptedPassword).replace("${salt}",salt).replace("${uid}",uid).replace("${idCardNo}",idCardNo).replace("${realName}",username).replace("${nickName}",username)
        return eval(newparam)
    if "${phone+1}" in param or "${pid}" in param:
        newphone = long(phone) + 1
        print newphone
        username = str(register.name())
        Apply_code = purchaseApply.submit(str(newphone), username, 'PASSED', '3')[1]  # 获得合伙人付款码
        newparam = param.replace("${phone+1}", str(newphone)).replace("${pid}", Apply_code)
        return eval(newparam)
    if "${phone+2}" in param or "${pid}" in param:
        newphone = long(phone) + 2
        print newphone
        username = str(register.name())
        Apply_code = purchaseApply.submit(str(newphone), username, 'PASSED', '3')[1]  # 获得合伙人付款码
        newparam = param.replace("${phone+1}", str(newphone)).replace("${pid}", Apply_code)
        return eval(newparam)

#从数据库获取headers，并且进行参数化
def Get_headers(id,phone):
    headers = Get_datas.Get_testdatas(id, 'headers')
    if "${uid}" in headers or "${Authorization}" in headers:
        uid = mysql_password.password(phone)[1]
        Authorization = str(platform_login_api.login(phone)[0])
        newheaders = headers.replace('{"Uid":"${uid}","Authorization":"${Authorization}"}', Authorization)
        return eval(newheaders)
    else:
        return ''

#从数据库获取url，并且进行参数化
def Get_url(id,phone):
    url = Get_datas.Get_testdatas(id,'url')
    if "${commentId}" in url or "${replyid}" in url or "${comid}"in url:
        commentId = str(reply.comment(phone, '测试评论一次', '1', '882')[1])
        reply_id = reply.reply(phone,'回复评论一次','1',commentId)[1]
        comid = str(mysql_password.community_num(phone))
        newurl = url.replace("${commentId}",commentId).replace("${replyid}",reply_id).replace("${comid}",comid)
        return newurl
    if "${paymentCode}" in url:
        username = str(register.name())
        Apply_code = purchaseApply.submit(phone, username, 'PASSED', '3')[1]  # 获得合伙人付款码
        newurl = url.replace("${paymentCode}", Apply_code)
        return newurl
    else:
        return url

#更新实际结果sql
def update(id,result):
    # 将实际结果更新至actual_result中
    connect = mysql_connect.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 更新语句
    sql = "update mysql.test_data set actual_result='" + result + "'WHERE caseid =" + id
    # 执行sql语句
    input = cursor.execute(sql)
    connect.commit()  # 实例提交命令

def update_pass(id):
    connect = mysql_connect.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 更新语句
    sql_pass = "update mysql.test_data set result='Pass' WHERE caseid =" + id
    # 执行sql语句
    input = cursor.execute(sql_pass)
    connect.commit()  # 实例提交命令

def update_fail(id):
    connect = mysql_connect.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 更新语句
    sql_fail = "update mysql.test_data set result='Fail' WHERE caseid =" + id
    # 执行sql语句

    input = cursor.execute(sql_fail)
    connect.commit()  # 实例提交命令


if __name__ == '__main__':
    phone_num = random.randint(10000000,99999999)
    basePhone = '138'+str(phone_num)

    list = register.password(str(basePhone),'cq123321')
    encryptedPassword = str(list[0])
    code = str(list[1])
    salt = str(list[2])

    for i in range(1,94,1):
        test(str(i),str(basePhone),encryptedPassword,code,salt)


    #Get_param('3','13800010196')